// pages/personal/personal.js
let api = require("../../utils/api.js");
let util = require("../../utils/util.js");
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow:wx.getStorageSync("token") , // 判断是否登录
    new_isShow:"",
    user_name:wx.getStorageSync("user_name"),// 用户名
    user_head:wx.getStorageSync("user_head"), // 用户头像
    user_state:"已登录", // 登录状态
    icon_arr:[  // icon
      {
      name:"待付款",
      icon_url:"../../images/fukuan.png"
    },
    {
      name:"待收获",
      icon_url:"../../images/shouhuo.png"
    },
    {
      name:"待评价",
      icon_url:"../../images/pingjia.png"
    },
    {
      name:"退换/售后",
      icon_url:"../../images/shuo.png"
    },
    {
      name:"全部订单",
      icon_url:"../../images/dingdan.png"
    }
  ],
  icon_arr2:[  // icon
    {
    name:"物流",
    icon_url:"../../images/wuliu.png"
  },
  {
    name:"抽奖",
    icon_url:"../../images/choujiang.png"
  },
  {
    name:"红包",
    icon_url:"../../images/hongbao.png"
  },
  {
    name:"充值",
    icon_url:"../../images/chongzhi.png"
  }
],
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(3333333,this.data.isShow)
  },
  // 微信登录
  getuserinfo(res) {
    console.log(res)
    
    if(wx.getStorageSync("token") === ""){
// 获取用户信息
let userInfo = res.detail;
// console.log(userInfo)
wx.getSetting({
  success: (res) => {
    console.log(res)
    // 判断用户是否点击允许授权
    if (res.authSetting["scope.userInfo"]) {
      // 微信小程序登录请求
      util.promise_ajax(api.login, "post", {
        appId: "wx25e421ffbe3f7b27",  // 自己的ID
        secret: "2fcc675ca3070a43a7b0789bdb146c45", // 密钥
        code: app.globalData.code,    // 登录的code
        userInfo: userInfo
      }).then((result) => {
        console.log(result.data.data.userInfo)
        console.log(result.data.data.userInfo.avatar)
        console.log(result.data.data.userInfo.nickname);
        
        if(result.data.errno ==0){
          console.log("登录", result);
          wx.setStorageSync("token", result.data.data.token);
          wx.setStorageSync("user_name",result.data.data.userInfo.nickname);
          wx.setStorageSync("user_head",result.data.data.userInfo.avatar)
          this.setData({
            isShow: result.data.data.token,
          }) 
          wx.showToast({
            title: '登录成功',
            icon: 'success',
            duration: 1500,
            mask: false,
          });
          this.setData({
            user_name:result.data.data.userInfo.nickname,          
          })
        }else{
          wx.showToast({
            title: '登录失败',
            icon:"none",
            duration: 1500,
            mask: false,
          });               
        }        
      })
    }
    
  },
  fail: () => { },
  complete: () => { }
});
    }else{
      wx.showToast({
        title: '已登录',
        icon:"none",
        duration: 1500,
        mask: false,
      }); 
    }
    


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})